﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Inspired.Euler
{
    /// <summary>
    /// Some positive integers n have the property that the sum [ n + reverse(n) ] consists entirely of odd (decimal) digits.
    /// 
    /// For instance, 36 + 63 = 99 and 409 + 904 = 1313. We will call such numbers reversible; 
    /// so 36, 63, 409, and 904 are reversible. Leading zeroes are not allowed in either n or reverse(n).
    /// 
    /// There are 120 reversible numbers below one-thousand.
    /// 
    /// How many reversible numbers are there below one-billion (109)?
    /// </summary>
    public static class Problem145
    {
        /// <summary>
        /// How many reversible numbers are there below one-billion?
        /// </summary>
        [EulerProblem(145, Title = "How many reversible numbers are there below one-billion?")]
        public static long Solve()
        {
            long count = 0;
            long oneBillion = (long)Math.Pow(10, 9);
            for (long n = 1; n < oneBillion; n++)
            {
                if (n % 10 == 0)
                {
                    if(n % 1000000 == 0)
                        Console.Title = n.ToString("0,000");
                    continue;
                }

                long result = n + n.Reverse();
                if(allOdd(result))
                    count++;
            }
            return count;
        }

        static bool allOdd(long n)
        {
            bool res = true;
            while (res && n > 0)
            {
                res = (n % 10) % 2 == 1;
                n = n / 10;
            }
            return res;
        }
    }
}
